using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Testing_UpdateCoords : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        PlacementManager.AddressComponent oAddressC = new PlacementManager.AddressComponent();
        List<PlacementManager.Address> oAddressList = oAddressC.SelectAll("Address1");

        string address;
        string Address1;
        string Address2;
        string City;
        string State;
        string Country;

        PlacementManager.Geo_CountryComponent oGeoCountryC = new PlacementManager.Geo_CountryComponent();
        PlacementManager.Geo_AdministrativeDivisionComponent oStateC = new PlacementManager.Geo_AdministrativeDivisionComponent();
        PlacementManager.GoogleMap oGM = new PlacementManager.GoogleMap();

        foreach (PlacementManager.Address oAddress in oAddressList)
        {
            if (oAddress.Longitude == 0)
            {
                Address1 = oAddress.Address1;
                Address2 = oAddress.Address2;
                City = oAddress.City;
                State = oStateC.SelectOne(oAddress.StateId).DivisionName;
                Country = oGeoCountryC.SelectOne(oAddress.CountryId).CountryName;

                address = Address1 + ", ";
                if(Address2.Length>0)
                {
                    address += Address2 + ", ";
                }
                address += City + ", " + State + ", " + Country;

                float[] coordinates = oGM.GetLongitudeLatitude(address);
                oAddress.Longitude = coordinates[0];
                oAddress.Latitude = coordinates[1];

                oAddressC.Update(oAddress);
            }
        }
    }
}
